package la.iok.hzsvn.lewin.movie.controller;

import la.iok.hzsvn.lewin.movie.entity.MovieAssistImage;
import la.iok.hzsvn.lewin.movie.entity.MovieInstance;
import la.iok.hzsvn.lewin.movie.model.FileVo;
import la.iok.hzsvn.lewin.movie.service.MovieAssistImageService;
import la.iok.hzsvn.lewin.movie.service.MovieInstanceService;
import la.iok.hzsvn.share.response.ListDataResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("movieAssistImages")
public class MovieAssistImageController {
    private final MovieInstanceService movieInstanceService;
    private final MovieAssistImageService movieAssistImageService;

    public MovieAssistImageController(MovieInstanceService movieInstanceService,
                                      MovieAssistImageService movieAssistImageService) {
        this.movieInstanceService = movieInstanceService;
        this.movieAssistImageService = movieAssistImageService;
    }

    @GetMapping("movie/{movieId}/v/{version}")
    public ListDataResponse<FileVo> list(@PathVariable Long movieId, @PathVariable String version){
        MovieInstance mi = movieInstanceService.movieInst2(movieId,version);
        List<MovieAssistImage> images = movieAssistImageService.listByProperty("movieInstanceId",mi.getId());
        return new ListDataResponse<>(images.stream().map(po->{
            FileVo vo = new FileVo();
            BeanUtils.copyProperties(po,vo);
            return vo;
        }).collect(Collectors.toList()));
    }

    /**
     * 查询图片文件摘要信息
     * @param id 角色id
     * @return 角色文件摘要信息
     */
    @GetMapping("{id}/digest")
    public SingleDataResponse<String> digest(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(movieAssistImageService.digest(id));
    }

    /**
     * 查询图片文件存储路径
     * @param id 角色id
     * @return 角色文件存储路径
     */
    @GetMapping("{id}/storePath")
    public SingleDataResponse<String> storePath(@PathVariable Long id){
        return OperatorResponses.stringSingleDataResponse(movieAssistImageService.storePath(id));
    }

    /**
     * 下载图片文件
     * @param response 响应
     * @param id 角色id
     */
    @GetMapping("{id}/file")
    public void downloadFile(HttpServletResponse response, @PathVariable Long id){
        movieAssistImageService.downloadFile(response,id);
    }
}
